﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!-- 引入 layui.css -->
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="">
    <div style="padding: 20px;">
        <h3><i class="layui-icon layui-icon-edit"></i> 编辑工序</h3>
    </div>
    <input type="hidden" name="id" id="id" />
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工序编号</label>
            <div class="layui-input-block">
                <input type="text" name="processCode" id="processCode" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入工序编号">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">工序名称</label>
            <div class="layui-input-inline">
                <input type="text" name="processName" id="processName" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入工序名称">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工序分类</label>
        <div class="layui-input-inline">
            <select name="processType_Id" id="processTypeSelect" lay-filter="processType" lay-verify="required">
                <option value="">请选择工序分类</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="processStatus" value="true" title="启用">
            <input type="radio" name="processStatus" value="false" title="禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-inline">
            <select name="responsible" id="responsible" lay-filter="responsible">
                <option value="">请选择负责人</option>
                <option value="张三">张三</option>
                <option value="李四">李四</option>
                <option value="王五">王五</option>
                <option value="赵六">赵六</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-inline">
            <select name="department" id="department" lay-filter="department">
                <option value="">请选择部门</option>
                <option value="生产部">生产部</option>
                <option value="加工部">加工部</option>
                <option value="打包部">打包部</option>
                <option value="运输部">运输部</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" id="remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="submitEdit">保存</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 页面首次加载分类下拉
        loadProcessTypes();

        // 获取Id并加载详情
        var id = localStorage['processId'];
        if (!id) {
            layer.msg('未获取到要编辑的Id', { icon: 2 });
        } else {
            loadDetail(id);
        }

        function loadProcessTypes(selectedId) {
            $.ajax({
                url: 'http://localhost:5141/api/Process/ShowProcessType',
                type: 'GET',
                data: { pageindex: 1, pagesize: 100, processTypeCode: '', processTypeName: '' },
                success: function (res) {
                    var options = '<option value="">请选择工序分类</option>';
                    if (res && res.pageData && res.pageData.length > 0) {
                        $(res.pageData).each(function () {
                            if (this.processTypeStatus) {
                                options += '<option value="' + this.id + '">' + this.processTypeName + '</option>';
                            }
                        });
                    }
                    $('#processTypeSelect').html(options);
                    if (selectedId) {
                        $('#processTypeSelect').val(String(selectedId));
                    }
                    form.render('select');
                }
            });
        }

        function setSelectValue(selectId, value) {
            if (value == null || value === undefined) return;
            $('#' + selectId).val(String(value));
            form.render('select');
        }

        function loadDetail(id) {
            $.ajax({
                url: 'http://localhost:5141/api/Process/ProcessInfoDetail',
                type: 'GET',
                data: { id: id },
                success: function (res) {
                    if (res && res.code === 200 && res.data) {
                        $('#id').val(res.data.id);
                        $('#processCode').val(res.data.processCode || '');
                        $('#processName').val(res.data.processName || '');
                        // 分类先渲染后选中
                        loadProcessTypes(res.data.processType_Id);
                        // 单选状态
                        var status = res.data.processStatus === true ? 'true' : 'false';
                        $("input[name='processStatus'][value='" + status + "']").prop('checked', true);
                        // 负责人/部门
                        setSelectValue('responsible', res.data.responsible || '');
                        setSelectValue('department', res.data.department || '');
                        $('#remark').val(res.data.remark || '');
                        form.render();
                    } else {
                        layer.msg(res && res.message ? res.message : '加载详情失败', { icon: 2 });
                    }
                },
                error: function () { layer.msg('加载详情失败', { icon: 2 }); }
            });
        }

        // 提交事件（更新）
        form.on('submit(submitEdit)', function (data) {
            var field = data.field;
            var $btn = $('button[lay-filter="submitEdit"]');
            if ($btn.prop('disabled')) return false;

            if (!field.processCode || !field.processName || !field.processType_Id) {
                layer.msg('请填写所有必填字段', { icon: 2 });
                return false;
            }

            var request = {
                id: parseInt(field.id, 10),
                processCode: field.processCode.trim(),
                processName: field.processName.trim(),
                processType_Id: parseInt(field.processType_Id, 10),
                responsible: field.responsible ? field.responsible.trim() : '',
                department: field.department ? field.department.trim() : '',
                processStatus: field.processStatus === 'true',
                remark: field.remark ? field.remark.trim() : null
            };

            $btn.prop('disabled', true).text('提交中...');
            $.ajax({
                url: 'http://localhost:5141/api/Process/UpdateProcessinfo',
                type: 'POST',
                data: JSON.stringify(request),
                contentType: 'application/json',
                success: function (res) {
                    $btn.prop('disabled', false).text('保存');
                    if (res && res.code === 200) {
                        layer.msg('更新成功', { icon: 1, time: 1200 }, function () {
                            if (window.parent && window.parent.loadPage) {
                                window.parent.loadPage('/Process/ProcessList', '工序管理列表');
                            }
                        });
                    } else {
                        layer.msg(res && res.message ? res.message : '更新失败', { icon: 2 });
                    }
                },
                error: function () {
                    $btn.prop('disabled', false).text('保存');
                    layer.msg('网络错误，请重试', { icon: 2 });
                }
            });
            return false;
        });
    });
</script>

